package com.service;

import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_PRD_INFO;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef._NORMAL;

import java.sql.Timestamp;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdOutDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_prd_info;
import com.model.Ret_prd_out;
import com.model.Ret_prd_outId;
import com.tx.Xpsamplg.XpsamplgI;
import com.tx.Xpsamplg.XpsamplgO;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpsamplingService")
public class XpsamplingService extends ICIMBaseService{
	private XpsamplgI inTrx ;
	
	private XpsamplgO outTrx ;
	
	private static Logger logger = Logger.getLogger(XpsamplingService.class);
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;
	
	
	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	
	public String subMainProc(String strInMsg) {
		
		long rtnCode = _NORMAL;

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XpsamplgI();
		outTrx = new XpsamplgO();
		
		outTrx.setTrx_id("XPSAMPLG");
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();

		try {
			inTrx = (XpsamplgI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}
	
	public long subMainProc2() {
		long rtnCode = _NORMAL;
		Ret_prd_info ret_prd_info = retPrdInfoDao.get(inTrx.getPrd_seq_id_fk());
		if(ret_prd_info==null){
			outTrx.setRtn_code( E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("not Found Record in Ret_prd_info id["+inTrx.getPrd_seq_id_fk() +"]");
			return E_RET_PRD_INFO + E_READ_NOT_FOUND ;
		}
		Ret_prd_out ret_prd_out   = new Ret_prd_out();
		Ret_prd_outId ret_prd_outId = new Ret_prd_outId();
		ret_prd_outId.setPrd_seq_id_fk(inTrx.getPrd_seq_id_fk());
		ret_prd_outId.setEvt_seq_id_fk(GUIDGenerator.javaGUID());
		ret_prd_out.setId(ret_prd_outId);
		ret_prd_out.setRul_act_code(inTrx.getRul_act_code());
		ret_prd_out.setFr_path_id_fk(inTrx.getFr_path_id_fk());
		ret_prd_out.setFr_path_ver_fk(inTrx.getFr_path_ver_fk());
		ret_prd_out.setFr_ope_no_fk(inTrx.getFr_ope_no_fk());
		ret_prd_out.setTo_wo_id_fk(inTrx.getTo_wo_id_fk());
		ret_prd_out.setTo_mdl_id_fk(inTrx.getTo_mdl_id_fk());
		ret_prd_out.setAction_id(inTrx.getAction_id());
		ret_prd_out.setAction_ver(inTrx.getAction_ver());
		ret_prd_out.setTo_path_id_fk(inTrx.getTo_path_id_fk());
		ret_prd_out.setTo_path_ver_fk(inTrx.getTo_path_ver_fk());
		ret_prd_out.setTo_ope_no_fk(inTrx.getTo_ope_no_fk());
		ret_prd_out.setTo_tool_id_fk(inTrx.getTo_tool_id_fk());
		ret_prd_out.setOut_path_id_fk(inTrx.getOut_path_id_fk());
		ret_prd_out.setOut_path_ver_fk(inTrx.getOut_path_ver_fk());
		ret_prd_out.setOut_ope_no_fk(inTrx.getOut_ope_no_fk());
		ret_prd_out.setRsn_cate(inTrx.getRsn_cate());
		ret_prd_out.setRsn_code(inTrx.getRsn_code());
		ret_prd_out.setRsn_dept(inTrx.getRsn_dept());
		ret_prd_out.setOpe_msg(inTrx.getOpe_msg());
		retPrdOutDao.save(ret_prd_out);
		return rtnCode;
	}
	
}
